<script setup>
import {
    Edit,
    Delete,
    Close
} from '@element-plus/icons-vue'


import { ref } from 'vue'


const types = ref([
    {
        "id": 1,
        "contentValue": "等级",
    },
    {
        "id": 2,
        "contentValue": "里程碑",
    },
    
])

const type = ref('')




const versionSeasons = ref([


])




import {versionSeasonListService ,versionSeaonAddService,versionSeasonService} from '@/api/versionSeason.js'



import { Plus } from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const versionSeasonModel = ref({
    type:1
})


//导入token
import { useTokenStore } from '@/stores/token.js';
const tokenStore = useTokenStore();


//上传成功的回调函数
const uploadSuccess = (result) => {
    versionSeasonModel.value.imgUrl = result.data;
    console.log(result.data);
}

const uploadAudioSuccess = (result) => {
    versionSeasonModel.value.audioUrl = result.data;
    console.log(result.data);
}


//添加文章
import { ElMessage } from 'element-plus'
const addVersionSeason = async (clickState) => {
    //把发布状态赋值给数据模型
    versionSeasonModel.value.state = clickState;

    //调用接口
    let result = await versionSeaonAddService(versionSeasonModel.value);

    ElMessage.success(result.msg ? result.msg : '添加成功');

    //让抽屉消失
    visibleDrawer.value = false;

    //刷新当前列表
    versionSeasonList()
}

//编辑分类
const updateVersionSeason = async () => {
    //调用接口
    let result = await versionSeasonService(versionSeasonModel.value);

    ElMessage.success(result.msg ? result.msg : '修改成功')

    //调用获取所有分类的函数
    versionSeasonList();

    //隐藏弹窗
    visibleDrawer.value = false;
}
type.value=1;


//获取文章列表数据
const versionSeasonList = async () => {
    let params = {
        type: type.value ? type.value : null

    }

    let result = await versionSeasonListService(params);
    versionSeasons.value = result.data;

}


versionSeasonList();



//定义变量,控制标题的展示
const title = ref('')
const showVersionSeason = (row) => {
    
    visibleDrawer.value = true, title.value = '编辑版本/赛事设置'
    versionSeasonModel.value.type = row.type;
    versionSeasonModel.value.num = row.num;
    versionSeasonModel.value.name = row.name;
    versionSeasonModel.value.simpleName = row.simpleName;
    versionSeasonModel.value.fromTime=row.fromTime;
    versionSeasonModel.value.endTime=row.endTime;
    versionSeasonModel.value.id = row.id;


}

//清空模型的数据
const clearData = () => {

    versionSeasonModel.value.type = 1;
    versionSeasonModel.value.num = '';
    versionSeasonModel.value.name = '';
    versionSeasonModel.value.simpleName = '';
    versionSeasonModel.value.fromTime='';
    versionSeasonModel.value.endTime='';
    versionSeasonModel.value.id = '';
}

const handleChange = (value) => {
   if(versionSeasonModel.value.type ==1){
    versionSeasonModel.value.name = "第"+value+"版本";
    versionSeasonModel.value.simpleName = 'g'+value;
   }else{
    versionSeasonModel.value.name = "第"+value+"赛事";
    versionSeasonModel.value.simpleName = 's'+value;
   }
}



</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>版本/赛事设置</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer = true; title = '添加版本/赛事'; clearData()">添加版本/赛事</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="类型：">
                <el-select placeholder="请选择" v-model="type">
                    <el-option v-for="c in types" :key="c.id" :label="c.contentValue" :value="c.id">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="versionSeasonList">搜索</el-button>
                <el-button @click="type = ''">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 文章列表 -->
        <el-table :data="versionSeasons" border  style="width: 100%" @selection-change="handleSelectionChange">
        
            <el-table-column label="类型">
                <template v-slot="scope">
                    <span v-if="scope.row.type == 1">等级</span>
                    <span v-else>里程碑</span>
                </template>
            </el-table-column>
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="简称" prop="simpleName"></el-table-column>
            <el-table-column label="开始时间" prop="fromTime"></el-table-column>
            <el-table-column label="结束时间" prop="endTime"></el-table-column>
            <el-table-column label="创建时间" width="200px" prop="createTime"></el-table-column>
            <el-table-column prop="num" v-if="showNum" ></el-table-column>
            <el-table-column prop="id" v-if="showId" ></el-table-column>
            <el-table-column label="操作"  width="150px">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showVersionSeason(row)"></el-button>
                
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>




        <!-- 抽屉 -->
        <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
            <!-- 添加文章表单 -->
            <el-form :model="versionSeasonModel" label-width="100px">
                <el-form-item label="类型">
                    <el-select placeholder="请选择" v-model="versionSeasonModel.type">
                        <el-option v-for="c in types" :key="c.id" :label="c.contentValue" :value="c.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="阶段">
                    <el-input-number v-model="versionSeasonModel.num" :min="1" @change="handleChange" />
                </el-form-item>
                <el-form-item label="名称"   v-model="versionSeasonModel.name">
                    <span>{{ versionSeasonModel.name }}</span>
                </el-form-item>

                <el-form-item label="简称" v-model="versionSeasonModel.simpleName" >
                    <span>{{ versionSeasonModel.simpleName }}</span>
                </el-form-item>
                <el-form-item label="开始时间" >
                    <el-date-picker v-model="versionSeasonModel.fromTime"  type="date" value-format="YYYY-MM-DD">
                </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" >
                    <el-date-picker v-model="versionSeasonModel.endTime"  type="date" value-format="YYYY-MM-DD">
                </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="title == '添加版本/赛事' ? addVersionSeason() : updateVersionSeason()">保存</el-button>

                </el-form-item>
            </el-form>
        </el-drawer>


    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}


/* 抽屉样式 */
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}

.editor {
    width: 100%;

    :deep(.ql-editor) {
        min-height: 200px;
    }
}
</style>